{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4e770160",
   "metadata": {},
   "source": [
    " ·2022/09/20/week05\n",
    " ·Python课堂笔记\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3528b8a3",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "1.有序的数据结构之列表\n",
    "\n",
    "2.用户输入\n",
    "\n",
    "3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d1b536f",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name=\"Marry\"\n",
    "#思考：如果想一个变量存放一个班同学的名字，该怎么办？\n",
    "#思考：处理数据会不会比较麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "da360644",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Alex,Jack,周晓明\n"
     ]
    }
   ],
   "source": [
    "网三_name_list=\"Marry,Alex,Jack,周晓明\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bd943933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思考2:处理数据会不会比较麻烦？\n",
    "网三_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "192ee10b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思考3:我想知道这个名单有多少个人名？\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85588e3c",
   "metadata": {},
   "source": [
    "# #1.引入列表的概念\n",
    "* Lists might contain items of different types,but usually the items all have the same type\n",
    ">注释：我们已经学习了  数值和  字符，list可以放数值和字符，但是通常要嘛一起放数值，要嘛一起放字符串。\n",
    "\n",
    ">总结一些通用的有序数据结构的方法:\n",
    ">>1.取值:通过索引值取出对应的内容\n",
    ">>2.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6e308a77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7d2313c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Alex', 'Jack', '周晓明'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list=[\"Marry\",\"Alex\",\"Jack\",\"周晓明\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07fb6eb6",
   "metadata": {},
   "source": [
    "### 1.1列表的运算法则 (Operation)\n",
    "* <font = 列表的数据本身不做修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8edff60a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取出 网三_name_list 中第一位同学的名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "88724d2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看 网三_name_list 有多少位同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "30990036",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三： Marry\n",
      "网三： Alex\n",
      "网三： Jack\n",
      "网三： 周晓明\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三：\", i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b7288b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "#关于运算法则（in  +切片 取值len(s)s.count）\n",
    "网三_name_list_转专业=[\"Xiaoming\",\"Lihua\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "99debe01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', 'Xiaoming', 'Lihua']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list +网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "53c31961",
   "metadata": {},
   "outputs": [],
   "source": [
    "#思考：一个list  可不可以出现 重复项？\n",
    "网一_age_list =[18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8225a30c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20802586",
   "metadata": {},
   "source": [
    "# 2.2列表的方法(Function)--对列表数据进行操作\n",
    "\n",
    " 1.增\n",
    " 2.删\n",
    " 3.改\n",
    " 4.查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "de5cc0c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3a0a2609",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"张志豪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "70713233",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', '张志豪']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c3c2f47c",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"王一名\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "afd6dea9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', '张志豪', '王一名']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ccb64f2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5e893860",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f7802069",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "760757c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Alex', 'Jack', '周晓明', '张志豪', '王一名']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "bd2b77f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d844232f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "3b3350af",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'romove'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[0;32mIn [34]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43m网三_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mromove\u001b[49m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mAlex\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'romove'"
     ]
    }
   ],
   "source": [
    "网三_name_list.romove('Alex')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f41eaacf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '周晓明', '张志豪', '王一名']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#remove 经常和  in 一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Alex\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"Alex\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5279f38c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1bdff41e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "ccc33ef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '周晓明', '张志豪', '王一名']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3f5fdd29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'王一名'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b0e170cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "3dcf6fe6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '周晓明', '张志豪']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "b1b84ce9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#实践请移除19的元素\n",
    "age_list=[18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "c2a57565",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "cdd0a8f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "842c0976",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思路1:remove(value),先决条件，知道值出现的次数\n",
    "删除的数量=age_list.count(19)\n",
    "for i in range(删除的数量)：\n",
    "if 19 in age_list:\n",
    "    age_list.remove(19)\n",
    "    age_list\n",
    "age_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "2ec256f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list=[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "510b4a9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "670ecb40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "6 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思路2:pop(index),先决条件，知道值出现的位置\n",
    "for i,v in enumerate (age_list):\n",
    "    if v==19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "268e18f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c38d1b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
